﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcs.Web.project
{
    public partial class Addmaterial : Web.Admin.BasePage
    {
        public string dogid = "NO";
        public string Decode(string str)
        {
            if (dogid == "NO")
            {
                if (Request.Cookies["dogid"] != null)
                    dogid = Request.Cookies["dogid"].Value.ToString();
            }
            return DecodeDog(str, dogid);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
                BindGV2();
                Page.DataBind();
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void btnadd_Click(object sender, EventArgs e)
        {
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int out_storage_detail_Id = 0,id=0;
                int.TryParse(Request.Params["id"].ToString(), out out_storage_detail_Id);
                decimal nums = 0;
                decimal.TryParse(txtnums.Text, out nums);
                string idlist = "";
                int out_storageList_id = 0;
                string oslid = "";
                for (int i = 0; i < gridView.Rows.Count; i++)
                {
                    CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                    if (ChkBxItem != null && ChkBxItem.Checked)
                    {                        
                        //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                        if (gridView.DataKeys[i].Value != null)
                        {
                            idlist = gridView.DataKeys[i][0].ToString();
                            oslid = gridView.DataKeys[i][1].ToString();
                            int.TryParse(idlist, out id);
                            int.TryParse(oslid, out out_storageList_id);
                            Model.fmmateriallink model = new Model.fmmateriallink();
                            model.nums = nums;
                            model.material_id = id;
                            model.inputtime = DateTime.Now;
                            model.out_storage_detail_id = out_storage_detail_Id;
                            model.out_storageList_id = out_storageList_id;
                            BLL.fmmateriallink fbll = new BLL.fmmateriallink();
                            fbll.Add(model);
                            BindGV2();
                        }
                    }
                }
            }
        }
        public void BindData()
        {
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != ""&& Request.Params["pn"] != null && Request.Params["pn"].Trim() != "")
            {
                int projectId = (Convert.ToInt32(Request.Params["id"]));
                string projectName = Request.Params["pn"];
                projectName = System.Web.HttpUtility.UrlDecode(projectName);
                DataSet ds = new DataSet();
                StringBuilder strWhere = new StringBuilder();
                if (txtKeyword.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and material like '%{0}%'", txtKeyword.Text.Trim());
                }
                strWhere.Append(string.Format("and target_project='{0}'", projectName));
                lcs.BLL.out_storageList obll = new BLL.out_storageList();
                ds = obll.GetListVn(strWhere.ToString());
                gridView.DataSource = ds;
                gridView.DataBind();
                
            }
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            int ID = (int)GridView2.DataKeys[e.RowIndex].Value;
            BLL.fmmateriallink fbll = new BLL.fmmateriallink();
            fbll.Delete(ID);
            BindGV2();
        }
        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }
        private void BindGV2()
        {
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "" )
            {
                int projectId = (Convert.ToInt32(Request.Params["id"]));
                lcs.BLL.fmmateriallink odm = new BLL.fmmateriallink();
                StringBuilder strWhere = new StringBuilder();                
                strWhere.Append(string.Format("and out_storage_detail_id={0}", projectId));                
                DataSet ds = odm.GetList(strWhere.ToString());
                this.GridView2.DataSource = ds;
                this.GridView2.DataBind();
            }
        }
    }
}